/*
    * @module:views/budget/investBudget/modulation
    * @author:silas
    * @date:2020-12-17
    * 预算下达
*/
<template>
  <div class="budgetRelease_container">
    <div class="contentTopTitle">
      <div class="fl">
        <div class="title">预算调整</div>
      </div>
    </div>
    <div>
        <div class="div" style="display: flex;margin-top: 20px;">
            <div class="tableBox">
                <el-table
                    height="73vh"
                    ref="table"
                    size="mini"
                    :header-cell-style="{ background: '#eef1f6' }"
                    :data="tableData"
                    v-loading="tableLoading"
                    border
                >
                    <el-table-column width="120" label="专业" prop="indexName"></el-table-column>
                    <el-table-column width="120" label="下达" prop="indexValue"></el-table-column>
                    <el-table-column v-if="modifyValues.length" width="120" v-for="(it, i) in modifyValues" :key="i" :label="it.label" :prop="it.value"></el-table-column>
                    <el-table-column width="120" label="调整" >
                        <template slot-scope="scope">
                            <span v-if="type == 'change'" >
                                <el-input :disabled="scope.$index == 0" stype="text" min="0" @input="checkData(scope.row, scope.$index)" v-model="scope.row[scope.row.indexCode]"></el-input>
                            </span>
                            
                            <span v-if="type == 'check' || type == 'detail'">{{scope.row.modifyValue}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column width="120" label="偏差" prop="diffValue"></el-table-column>
                </el-table>
       
            </div>
            <div class="textarea">
                <div class="title">说明:</div>
                <div>
                    <textarea :disabled="type == 'detail'" maxlength="200" class="desc" v-model="desc"></textarea>
                </div>
                <div v-if="type == 'check'">
                    <el-form :model="queryForm">
                        <el-form-item label="审核意见:" label-width="70">
                            <el-radio v-model="queryForm.status" label="2">通过审核</el-radio><el-radio v-model="queryForm.status" label="3">未通过审核</el-radio>
                        </el-form-item>
                    </el-form>
                    <div class="">
                         <el-button size="mini" type="primary" @click="handle_check()">确定</el-button><el-button size="mini" @click="$router.push({path: '/budget/budgetRelease', query: {type: 'modulationList' } });">取消</el-button>
                    </div>
                </div>
            </div>
        </div>
        <div class="g-footer" v-if="type == 'change'">
            <el-button size="mini" type="primary" @click="handle_save(0)">保存</el-button>
            <el-button size="mini" type="primary" @click="handle_save(1)">保存并提交</el-button>
            <el-button size="mini" @click="$router.push({path: '/budget/budgetRelease', query: {type: 'change' } });">取消</el-button>
        </div>
        <div class="g-footer" v-if="type == 'detail'"> 
            <el-button size="mini" @click="$router.push({path: '/budget/budgetRelease', query: {type: 'modulationList' } });">关闭</el-button>
        </div>
    </div>
  </div>
</template>

<script>
import { $budgetAPI } from '@/api/';
import { getSessionStorage } from '@/common/js/storage';
import prefixAPI from '@/api/prefixAPI';
import { getCookie } from '@/common/js/cookie';
export default {
    data() {
        return {
            form: {
                areaId: '',
                year: this.moment().format('YYYY'),
                specialId: '' 
            },
            tableData: [],
            accountInfo: null,
            tableLoading: false,
            lvl: false,
            desc: '',
            id: '',
            type: '',
            queryForm: {
                status: '2'
            },
            recordId: '',
            ownCount: '',
            state: '',
            modifyValues: [],
        };
    },
    mounted() {
        var that = this;

        that.accountInfo = JSON.parse(getSessionStorage('accountInfo'));
        that.form.areaId = that.accountInfo.areaId;
        that.lvl = that.accountInfo.areaLvl;
        that.getInitData();
    },
    methods: {
       
        // 初始化
        async getInitData() {
            let that = this;
            that.getList();
            
        },
        getList(){
            var that = this, 
                id = that.$route.query.id, 
                type = that.$route.query.type, 
                obj = {}, 
                name = '';

            that.id = id;
            that.type = type;
            if(type == 'change') { // 下调 
                obj = {
                    'releaseId': id,
                    token: getCookie('token')
                }
                name = 'getReleaseModifyDataList';
            } 
            else if(type == 'detail' || type == 'check') {
                obj = {
                    'recordId': id,
                    token: getCookie('token')
                }
                name = 'getJiLuRecordDetailList'; 
            } 
            $budgetAPI[name](obj, res => {
                console.log(res);
                that.ownCount = res.content.data[0].indexValue;
                res.content.data[0].modifyValues && res.content.data[0].modifyValues.map((it, i)=> {
                    var obj = {
                        'label': '调整' + (i + 1),
                        'value': 'value' + i
                    }
                    that.modifyValues.push(obj)
                })
                res.content.data.map(it => {
                    it[it.indexCode] = it.modifyValue;
                    it.modifyValues && it.modifyValues.map((item, i)=> {
                        it['value' + i] = item;
                    })
                })
                that.tableData = res.content.data;
                that.desc = res.content.modifyRecord && res.content.modifyRecord.auditExplain;
                that.desc = res.content.updateExplain && res.content.updateExplain;
                if(type == 'detail') {
                    that.desc = res.content.modifyRecord && res.content.modifyRecord.updateExplain;
                    that.state = res.content.modifyRecord.status;
                }
                that.recordId = res.content.recordId;
            })
        },
        handle_save(type) {
            var that = this, 
                obj = [], 
                len = that.tableData.length, 
                j = 0, total = 0;

            that.tableData.map((it, i) => {
                var temp = {};
                temp[it.indexCode] = it[it.indexCode];
                obj.push(temp);
                // obj[it.indexCode] = it[it.indexCode];

                if(it[it.indexCode] && it[it.indexCode].toString().length) {
                    j = j + 1;
                }
                if( i > 0) { // sum 
                    total += +temp[it.indexCode]
                }
            })
            // if(j < len) {
            //     this.$message({
            //         type: 'error',
            //         message: '请输入全部调整内容'
            //     });
            //     return
            // }
            if (total != that.ownCount) {
                this.$message({
                    type: 'error',
                    message: '调整合计偏差不为0'
                });
                return
            }
            if (!that.desc) {
                that.$message({
                    type: "error",
                    message: "请填写调整说明",
                });
                return
            }
            // var obj1 = [];
            // obj.map((it, i)=> {
            //     if(it['H000' + (i +1)] != null) {
            //         obj1.push(it);
            //     } 
            // })
            $budgetAPI.getReleaseModify({
                token: getCookie('token'),
                modifyReleaseStr: JSON.stringify(obj),
                isSubmit: type,
                releaseId: that.id,
                recordId: that.recordId,
                updateExplain: that.desc
            }, res => {
                if(res.code == 0) {
                that.$message({
                    type: "success",
                    message: "操作成功",
                });
                that.$router.push({ path: '/budget/budgetRelease',query: {type: 'change' } })
                } else {
                    that.$message({
                        type: "error",
                        message: res.message,
                    });
                }
            })
        },
        checkData(it, i) {
            var that = this, total = 0;

            if(+it[it.indexCode] >=0 ) {
                that.tableData[i].diffValue = (+it[it.indexCode]) - it.indexValue;
                that.tableData.map((item, j) => {
                    console.log(item);
                    if (j > 0) {
                        total += +item[item.indexCode];
                    }
                });
                that.tableData[0].H0001 = total;
                that.tableData[0].diffValue = total - that.tableData[0].indexValue;
            } else {
                 this.$message({
                    type: 'error',
                    message: '请输入内容为正整数'
                });
            }
        },
        handle_check() {
            var that = this;

            if(!that.queryForm.status) {
                that.$message({
                    type: "error",
                    message: "请选择审核意见",
                });
                return false;
            }
            if(that.queryForm.status == 3 && !that.desc) {
                that.$message({
                    type: "error",
                    message: "未通过审核需填写意见说明",
                });
                return false;
            }
            $budgetAPI.getJiLuRecordCheck({
                recordId: that.id,
                status: that.queryForm.status,
                auditExplain: that.desc,
                token: getCookie('token'),
            }, res => {
            if(res.code == 0) {
                that.$message({
                    type: "success",
                    message: "审核成功",
                });
                that.$router.push({ path: '/budget/budgetRelease', query: {type: 'modulationList' } })
            } else {
                that.$message({
                    type: "error",
                    message: res.message,
                });
            }
            
            })
        }
    }
};
</script>
<style lang="scss" scoped>
    .dialog-footer {
        display: flex;
        justify-content: center;
    }
    .textarea {
        margin-left: 20px;
        .title {
            font-weight: bold;
            font-size: 16px;
        }
        .desc {
            margin-top: 15px;
            width: 350px;
            height: 250px;
            resize: none;
            border: 1px solid #e4e4e4;
            padding: 3px 8px;
        }       
    }
    .g-footer {
        margin-top: 20px;
        padding-left: 50px;
    }
</style>